const TrafficReport = require('../models/Traffic_report');

//添加信息
const addTrafficReport = async ({
  username,
  type,
  address,
  sign,
  time,
  description,
  coordinates,
  status,
}) => {
  let result = await TrafficReport.create({
    username,
    type,
    address,
    sign,
    time,
    description,
    coordinates,
    status,
  });
  if (result) {
    return {
      msg: '信息添加成功',
      status: 1,
      result,
    };
  } else {
    return {
      msg: '信息添加失败',
      status: 0,
    };
  }
};

//查询所有信息
const getAllTrafficReport = async () => {
  let result = await TrafficReport.findAll();
  if (result) {
    return {
      msg: '查询成功',
      status: 1,
      result,
    };
  } else {
    return {
      msg: '查询失败',
      status: 0,
    };
  }
};

//修改状态
const updateTrafficReportStatus = async ({ id, status }) => {
  let result = await TrafficReport.findOne({ where: { id } });
  if (result) {
    result.status = status;
    await result.save();
    return {
      msg: '修改成功',
      status: 1,
    };
  } else {
    return {
      msg: '修改失败',
      status: 0,
    };
  }
};

module.exports = {
  addTrafficReport,
  getAllTrafficReport,
  updateTrafficReportStatus,
};
